import { nextTick } from 'vue'

/**
 * 滚动到指定元素
 * @param parent 父元素class/id
 * @param child 子元素class/id
 * @param top 距离顶部的距离
 */
export const scroll2el = (child: string, parent = '.container', top = 150) => {
  nextTick(() => {
    uni
      .createSelectorQuery()
      .select(parent)
      .boundingClientRect((data: any) => {
        console.log('data', data)

        uni
          .createSelectorQuery()
          .select(child)
          .boundingClientRect((res: any) => {
            console.log('child', res)

            if (res) {
              uni.pageScrollTo({ duration: 300, scrollTop: res.top - data.top - top })
            }
          })
          .exec()
      })
      .exec()
  })
}
